package com.scen.upload.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scen.upload.domain.Customer;
import com.scen.upload.domain.System;
import com.scen.upload.dto.CustomerDto;
import com.scen.upload.dto.EchartsDto;
import com.scen.upload.mapper.CustomerMapper;
import com.scen.upload.mapper.SystemMapper;
import com.scen.upload.service.CustomerService;
import com.scen.upload.vo.DataEntity;
import com.scen.upload.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SystemMapper systemMapper;


    /**
     * 分页查询客户
     *
     * @param customerDto
     * @return
     */
    @Override
    public DataGridView listCustomerPage(CustomerDto customerDto) {
        Page<Customer> page = new Page<>(customerDto.getPageNum(), customerDto.getPageSize());
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(customerDto.getCustomer()), Customer.COL_CUSTOMER, customerDto.getCustomer());
        queryWrapper.like(StringUtils.isNotBlank(customerDto.getSite()), Customer.COL_SITE, customerDto.getSite());
        queryWrapper.like(StringUtils.isNotBlank(customerDto.getAddress()), Customer.COL_ADDRESS, customerDto.getAddress());
        queryWrapper.like(StringUtils.isNotBlank(customerDto.getSourceofcustomer()), Customer.COL_SOURCEOFCUSTOMER, customerDto.getSourceofcustomer());
        queryWrapper.ge(null != customerDto.getBeginTime(), Customer.COL_CREATETIME, customerDto.getBeginTime());
        queryWrapper.le(null != customerDto.getEndTime(), Customer.COL_CREATETIME, customerDto.getEndTime());
        queryWrapper.orderByDesc(Customer.COL_ID);
        this.customerMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 根据ID查询客户
     *
     * @param customerId
     * @return
     */
    @Override
    public Customer getOne(Long customerId) {
        return this.customerMapper.selectById(customerId);
    }

    /**
     * 添加客户
     *
     * @param customerDto
     * @return
     */
    @Override
    public int addCustomer(CustomerDto customerDto) {
        Customer customer = new Customer();
        BeanUtil.copyProperties(customerDto, customer);
        customer.setFounder(customerDto.getSimpleUser().getUserName());
        customer.setCreatetime(DateUtil.date());
        System address = this.systemMapper.selectById(customerDto.getAddress());
        customer.setAddress(address.getDeployment());
        System city = this.systemMapper.selectById(customerDto.getCity());
        customer.setCity(city.getDeployment());
        return this.customerMapper.insert(customer);
    }

    /**
     * 修改客户
     *
     * @param customerDto
     * @return
     */
    @Override
    public int updateCustomer(CustomerDto customerDto) {
        Customer customer = this.customerMapper.selectById(customerDto.getId());
        BeanUtil.copyProperties(customerDto, customer);
        customer.setUpdatebody(customerDto.getSimpleUser().getUserName());
        customer.setUpdatetime(DateUtil.date());
        customer.setId(customerDto.getId());
        System address = this.systemMapper.selectById(customerDto.getAddress());
        customer.setAddress(address.getDeployment());
        System city = this.systemMapper.selectById(customerDto.getCity());
        customer.setCity(city.getDeployment());
        return this.customerMapper.updateById(customer);
    }

    /**
     * 根据ID删除客户
     *
     * @param customerIds
     * @return
     */
    @Override
    public int deleteCustomerByIds(Long[] customerIds) {
        List<Long> ids = Arrays.asList(customerIds);
        if (ids.size() > 0 && ids != null) {
            return this.customerMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    /**
     * 查询全部客户
     *
     * @return
     */
    @Override
    public List<Customer> list(String status) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Customer.COL_STATUS, status);
        queryWrapper.orderByDesc(Customer.COL_ID);
        return this.customerMapper.selectList(queryWrapper);
    }

    /**
     * 根据客户id查询对应的省份id/市区id
     *
     * @param customerId
     * @return
     */
    @Override
    public List<Integer> getAddressData(String customerId) {
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq(Customer.COL_ID, customerId);
        Customer customer = this.customerMapper.selectOne(customerQueryWrapper);
        String address = customer.getAddress();
        String city = customer.getCity();
        QueryWrapper<System> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq(System.COL_DEPLOYMENT, address);
        System addressSystem = this.systemMapper.selectOne(addressQueryWrapper);
        Integer addressId = null;
        try {
            addressId = addressSystem.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        QueryWrapper<System> cityQueryWrapper = new QueryWrapper<>();
        cityQueryWrapper.eq(System.COL_DEPLOYMENT, city);
        System citySystem = this.systemMapper.selectOne(cityQueryWrapper);
        Integer cityId = null;
        try {
            cityId = citySystem.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<Integer> list = new ArrayList<>();
        list.add(addressId);
        list.add(cityId);
        return list;
    }

    /**
     * 构造省份客户数量对应数据串
     *
     * @return
     */
    @Override
    public Map<String, Object> queryAddressOfCustomer() {
        List<EchartsDto> list = this.customerMapper.queryAddressOfCustomer();
        HashMap<String, Object> res = new HashMap<>();
        List<String> address = new ArrayList<>();
        List<Double> customer = new ArrayList<>();
        List<Double> site = new ArrayList<>();
        List<Double> sum = new ArrayList<>();
        for (EchartsDto echartsDto : list) {
            address.add(echartsDto.getAddress());
            customer.add(echartsDto.getCustomer());
            site.add(echartsDto.getSite());
            sum.add(echartsDto.getSum());
        }
        res.put("address", address);
        res.put("customer", customer);
        res.put("site", site);
        res.put("sum", sum);
        return res;
    }

    /**
     * 查询饼状图所需数据
     *
     * @return
     */
    @Override
    public List<DataEntity> queryCakeData() {
        List<DataEntity> res = this.customerMapper.queryCakeData();
        return res;
    }
}
